﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using V5_EShop.Data;
using V5_Common;

namespace V5_EShop.Admin.Book {
    public partial class Admin_BookTopicClass : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string act = InputHelper.SetString(Request["action"]);
                Bind_DropClassList();
                switch (act) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            DataTable dt = V5.SQL.Query_By("*", "Tbl_B_BookTopicClass", "", "ClassCode Asc", 0);

            DataView dv = dt.DefaultView;
            Pager.RecordCount = dv.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = dv;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = Pager.CurrentPageIndex - 1;
            pds.PageSize = Pager.PageSize;

            this.gvList.DataSource = pds;
            this.gvList.DataBind();
        }

        private void Bind_DropClassList() {
            DataTable dtClassList = V5.Class.GetDropDownList("Tbl_B_BookTopicClass", 0, "作为一级栏目");
            this.ddlClassID.DataSource = dtClassList;
            this.ddlClassID.DataTextField = "ClassTitle";
            this.ddlClassID.DataValueField = "ClassID";
            this.ddlClassID.DataBind();

        }

        private void Bind_DataEdit(int ClassID) {
            this.MultiViewList.ActiveViewIndex = 1;
            this.lblTitle.Text = "修改栏目";
            this.btnSave.Text = "保 存 修 改";
            DataTable dt = V5.SQL.Query_By("*", "Tbl_B_BookTopicClass", "ClassID=" + ClassID, "", 1);
            this.hfClassID.Value = ClassID.ToString();
            this.txtClassName.Text = dt.Rows[0]["ClassTitle"].ToString();
            this.ddlClassID.SelectedValue = dt.Rows[0]["ParentID"].ToString();
            this.txtInfo.Text = dt.Rows[0]["Info"].ToString();
        }

        private void Bind_DataDelete(int ClassID) {
            new ModelB_BookTopicClass(p => p.ClassID == ClassID).Delete();
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int hfClassID = InputHelper.SetNumber(this.hfClassID.Value);
            string ClassTitle = this.txtClassName.Text;
            int ParentID = int.Parse("0" + this.ddlClassID.SelectedValue);
            string Info = this.txtInfo.Text;

            ModelB_BookTopicClass model = new ModelB_BookTopicClass();
            if (hfClassID > 0) {
                model = new ModelB_BookTopicClass(p => p.ClassID == hfClassID);
            }
            model.ClassID = hfClassID;
            model.ClassTitle = ClassTitle;
            model.ParentID = ParentID;
            model.Info = Info;
            if (hfClassID == 0) {
                int ClassId = V5.Class.GetMaxId("Tbl_B_BookTopicClass", "ClassId");
                model.ClassID = ClassId;
                model.Add();
                V5.Class.ClassAddUpdate("Tbl_B_BookTopicClass", 1, ClassId, ParentID);
            }
            else if (hfClassID > 0) {
                model.Update();
                V5.Class.ClassUpdateUpdate("Tbl_B_BookTopicClass", 1, hfClassID, ParentID);
            }
            Response.Redirect("Admin_BookTopicClass.aspx");
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int ClassID = InputHelper.SetNumber(e.CommandArgument);
            this.Bind_DropClassList();
            switch (eName) {
                case "go":
                    Bind_DataEdit(ClassID);
                    break;
                case "del":
                    Bind_DataDelete(ClassID);
                    break;
                case "addsubclass":
                    this.MultiViewList.ActiveViewIndex = 1;
                    this.ddlClassID.SelectedValue = ClassID.ToString();
                    break;
                case "up":
                    V5.Class.MoveClass("Tbl_B_BookTopicClass", ClassID, 1);
                    this.Bind_DataList();
                    break;
                case "down":
                    V5.Class.MoveClass("Tbl_B_BookTopicClass", ClassID, -1);
                    this.Bind_DataList();
                    break;
            }
        }
    }
}